package com.tron.controller.account;

import com.tron.context.RedisContext;
import com.tron.dto.R;
import com.tron.entity.TronAccount;
import com.tron.utils.tron.TronAddress;
import com.tron.utils.tron.TronService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/account")
@Api(value = "Tron账户控制器",tags = "Tron账户控制器") //swagger
public class AccountController {

    @Autowired
    private TronService tronService;

    @Autowired
    private RedisContext redisContext;

    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/create")
    @ApiOperation(value = "创建账户", notes = "创建账户")
    public R<?> createAccount(@RequestParam(value = "number") Integer number){
        System.out.println(number);
        Map<String,String> map=new HashMap<>();
        for (int i = 0; i < number; i++) {
            TronAccount tronAddress = tronService.generateAddress();
            map.put(tronAddress.getAddress(),tronAddress.getPrivateKey());

            //创建一个 就缓存里面记一个值
            tronAddress.setCreateDate(new Date());

            //获取Redis的Key 用的地址做的key
            String key = redisContext.getCreateAddress(tronAddress.getPrivateKey());

            //存入redis
            redisTemplate.opsForValue().set(key,tronAddress);
        }
        return R.ok(map);
    }

}
